package com.cai.oamanager.config;

import com.cai.oamanager.entity.People;
import com.cai.oamanager.service.MessageService;
import com.cai.oamanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/1/16 10 : 08
 * @package：com.cai.oamanager.config
 */
@Component
public class MyIntercepter implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;

    public MyIntercepter(UserService userService){
        this.userService=userService;
    }
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        System.out.println("进入拦截器");
        People people = (People) request.getSession().getAttribute("people");
        if(people==null){
            try {
                request.getSession().setAttribute("loginerror","请先登录");

                response.sendRedirect("/router/login.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }
        return true;
    }
}
